package org.mms.controller;

import org.mms.pojo.dto.MaterialApplicationDto;
import org.mms.pojo.entity.MaterialApplication;
import org.mms.pojo.request.MaterialApplicationCreateRequest;
import org.mms.pojo.request.MaterialApplicationReviewRequest;
import org.mms.pojo.request.MaterialApplicationUpdateRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.MaterialApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/material-applications")
public class MaterialApplicationController {

    @Autowired
    private MaterialApplicationService applicationService;

    @GetMapping
    public ResponseMessage<List<MaterialApplicationDto>> list() {
        return ResponseMessage.success(applicationService.list()
                .stream()
                .map(MaterialApplication::convertToDto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MaterialApplicationDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(applicationService.getById(id)
                .convertToDto());
    }

    @PostMapping
    public ResponseMessage<MaterialApplicationDto> create(@RequestBody MaterialApplicationCreateRequest request) {
        return ResponseMessage.success(applicationService.create(request)
                .convertToDto());
    }

    @PutMapping
    public ResponseMessage<MaterialApplicationDto> update(@RequestBody MaterialApplicationUpdateRequest request) {
        return ResponseMessage.success(applicationService.update(request)
                .convertToDto());
    }


    @PutMapping("/review")
    public ResponseMessage<MaterialApplicationDto> review(@RequestBody MaterialApplicationReviewRequest request) {
        return ResponseMessage.success(applicationService.review(request)
                .convertToDto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        applicationService.delete(id);
        return ResponseMessage.success(null);
    }
}